package com.pyb.java.basemodule;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.android.arouter.launcher.ARouter;
import com.pyb.java.commonutil.activity.ActivityManager;
import com.pyb.java.commonutil.utils.StatusBarUtil;

import butterknife.ButterKnife;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

public abstract class BaseActivity extends AppCompatActivity {

    //保存网络请求队列，销毁界面时取消所有请求disposables.clear();
    protected CompositeDisposable disposables = new CompositeDisposable();
    private LoadingDialog loadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
//        transparentStatusBar();
        ARouter.getInstance().inject(this);
        ButterKnife.bind(this);
        getIntentData();
        initView();
        initEvent();
        ActivityManager.create(this);
    }

    protected abstract int getLayoutId();

    protected void initView() {
    }

    protected void initEvent() {
    }

    protected void getIntentData() {
    }

    protected void addDisposable(@NonNull Disposable d) {
        disposables.add(d);
    }

    protected void clearDisposable() {
        disposables.clear();
    }

    protected void showLoadingDialog() {
        if (loadingDialog == null) {
            loadingDialog = new LoadingDialog(this);
        }
        loadingDialog.show();
    }

    protected void closeLoadingDialog() {
        if (loadingDialog != null) {
            loadingDialog.cancel();
        }
    }

    /**
     * 设置状态栏白色
     */
    protected void transparentStatusBar(){
        StatusBarUtil.setColor(this,Color.parseColor("#ffffff"),255);
        StatusBarUtil.setLightMode(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityManager.destroy(this);
        clearDisposable();
    }

    /**
     * 设置输入法,如果当前页面输入法打开则关闭
     */
    public void hideKeyboardNoView() {
        View a = this.getCurrentFocus();
        if (a != null) {
            InputMethodManager imm = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            try {
                imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        hideKeyboardNoView();
    }
}
